#-*-encoding:utf-8-*-
import requests
import os,sys
import re

def getHtml(url,t=30,encode=0):
    try:
        kr = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Language': 'zh-CN,zh;q=0.8',
            'Accept-Encoding': 'gzip, deflate,sdch,br',
            'Cache-Control': 'max-age=0',
        }
        r = requests.get(url,headers=kr,timeout=t)
        r.raise_for_status()
        if encode == 0:
            encode = r.apparent_encoding
        r.encoding = encode
        return r.text
    except:
        return 0

def getImg(url,imgname,path=0):
    if path == 0:
        path = sys.path[0]
    if not os.path.exists(path):
        os.mkdir(path)
    img = requests.get(url)
    name = imgname.split('.')
    if len(name) < 2:
        imgname += '.'+url.split('.')[-1]
    f = open(path+'/'+imgname,"wb")
    print(path+'/'+imgname)
    f.write(img.content)
    f.close()

def getRe(r,body):
    rebody = re.findall(r,body)
    return rebody
